/**
* @file relay.c
* @brief Implementation file for the relay control module.
*
* This file contains the implementation of functions for controlling a relay.
* It includes functions to initialize the relay, turn it on and off, toggle its state,
* and get its current state. It also defines the GPIO pin used for controlling the relay.
* @author Maker Studio
* @license Apache 2.0
* This code is open-sourced under the Apache 2.0 license. When using open-source libraries
* or code, please ensure to acknowledge the original authors and sources.
**/

#include "../inc/relay.h"

/**
 * @brief Initialize the relay.
 */
void RelayInit(void)
{
    IoTGpioInit(RELAY_PIN);
    // IoTSetFunc(RELAY_PIN, IOT_IO_FUNC_GPIO_2_GPIO);
    IoTGpioSetDir(RELAY_PIN, IOT_GPIO_DIR_OUT);
    IoTGpioSetOutputVal(RELAY_PIN, IOT_GPIO_VALUE0);
    relay_off();
}

/**
 * @brief Turn on the relay.
 */
void relay_on(void)
{
    IoTGpioSetOutputVal(RELAY_PIN, IOT_GPIO_VALUE1);
    relay_state = true;
}

/**
 * @brief Turn off the relay.
 */
void relay_off(void)
{
    IoTGpioSetOutputVal(RELAY_PIN, IOT_GPIO_VALUE0);
    relay_state = false;
}

/**
 * @brief Toggle the relay state.
 */
void relay_toggle(void)
{
    if (relay_state)
    {
        relay_off();
    }
    else
    {
        relay_on();
    }
}

/**
 * @brief Get the current state of the relay.
 * @return Relay state.
 */
bool relay_get_state(void)
{
    return relay_state;
}
